<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2>高校信息库管理系统</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginForm">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名">
            <i slot="prefix" class="el-icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码">
            <i slot="prefix" class="el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
        </el-form-item>
        <div class="register-link">
          <router-link to="/register">还没有账号？立即注册</router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          try {
            // 调用登录 API
            const res = await this.$axios.post('/user/login', this.loginForm)
            localStorage.setItem('token', res.data.token)
            localStorage.setItem('userRole', res.data.role)
            localStorage.setItem('username', res.data.username)
            this.$router.push('/')
            this.$message.success('登录成功')
          } catch (error) {
            this.$message.error(error.message || '登录失败')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('https://s21.ax1x.com/2025/03/10/pENNuid.jpg');
  background-repeat: no-repeat; /* 防止图片重复平铺，可根据需要调整 */
  background-size: cover; /* 使图片覆盖整个元素，可根据需要调整 */
}

.login-card {
  width: 400px;
}

.login-card h2 {
  text-align: center;
}

.register-link {
  text-align: center;
  margin-top: 20px;
}
</style> 